/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.draw;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.List;
import java.util.ListIterator;
import java.util.Vector;

import br.ufmg.dees.insane.commons.view.DrawingArea;
import br.ufmg.dees.insane.model.geo.GCubicCurve;
import br.ufmg.dees.insane.model.geo.GCurve;
import br.ufmg.dees.insane.model.geo.GLine;
import br.ufmg.dees.insane.model.geo.GQuadCurve;

public class CurveDraw extends Draw {
	
	DrawingArea da;
	
//*****************************************************************************
	
	public CurveDraw(DrawingArea da, Object represents) {
		this.da = da;
		this.setRepresents(represents);
		Point p1 = da.getViewPoint( ((GCurve)represents).getFirstPoint() );
		this.setLocation(p1);
		Point p2 = da.getViewPoint( ((GCurve)represents).getEndPoint() );
		this.setSize(new Dimension(p2.x, p2.y));
	}
	
//*****************************************************************************
	
	public boolean equals(Object obj) {
		if (super.equals(obj) && obj instanceof CurveDraw) {
			CurveDraw d = (CurveDraw)obj;
			if (d.getP2().equals(getP2())){
				return true;
			}
		}
		return false;
	}
	
//*****************************************************************************
	
	public Point getP1() {
		return this.getLocation();
	}
	
//*****************************************************************************
	
	public Point getP2() {
		Point po = new Point(this.getSize().width, this.getSize().height);
		return po;
	}
	
//*****************************************************************************
	
	public double getLength() {
		double dx = this.getX2() - this.getX1();
		double dy = this.getY2() - this.getY1();
		double l = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
		return l;
	}
	
//*****************************************************************************
	
    public double getX1() {
        return getLocation().x;
    }
	
//*****************************************************************************
	
    public double getY1() {
        return getLocation().y;
    }
	
//*****************************************************************************
	
    public double getX2() {
        return getSize().width;
    }
	
//*****************************************************************************
	
    public double getY2() {
        return getSize().height;
    }
	
//*****************************************************************************
	
    public Vector<Object> getShapes() {
        return null;
    }
	
//*****************************************************************************
	
	public double getAngle() {
		double theta;
		double dx = getX2() - getX1();
		double dy = getY2() - getY1();
		theta = Math.atan2(-dy, dx);
		return theta;
	}
    
//*****************************************************************************
	
    /**
     * Draws in a given Graphics.
     *
     * @see   java.awt.Graphics
     */
	public void draw(Graphics2D g2) {
		GCurve gcv = (GCurve)getRepresents();
		GeneralPath curve = new GeneralPath();
		Point2D first = gcv.getFirstPoint();
		Point last = da.getViewPoint(first);
		Point p1, p2, p3;
		float x4 = (float)last.getX();
		float y4 = (float)last.getY();
		curve.moveTo(x4,y4);
		Draw draw;
		double angle = 0;
		List primitives = gcv.getPrimitives();
		ListIterator prs = primitives.listIterator();
		while(prs.hasNext()) {
			Object obj = prs.next();
			if (obj instanceof GLine) {
				Line2D prt = (Line2D)obj;
				p1 = da.getViewPoint(prt.getP2());
				float x1 = (float)p1.getX();
				float y1 = (float)p1.getY();
				curve.lineTo(x1,y1);
				draw = new LineDraw(last, p1, (GLine)obj);
				angle = draw.getAngle();
				last = p1;
			}
			if (obj instanceof GQuadCurve) {
				GQuadCurve prt = (GQuadCurve)obj;
				p1 = da.getViewPoint(prt.getCtrl());
				float x1 = (float)p1.getX();
				float y1 = (float)p1.getY();
				p2 = da.getViewPoint(prt.getP2());
				float x2 = (float)p2.getX();
				float y2 = (float)p2.getY();
				curve.quadTo(x1,y1,x2,y2);
				draw = new QuadCurveDraw(last, p1, p2, (GQuadCurve)obj);
				angle = draw.getAngle();
				last = p2;
			}
			if (obj instanceof GCubicCurve) {
				GCubicCurve prt = (GCubicCurve)obj;
				p1 = da.getViewPoint(prt.getCtrl1());
				float x1 = (float)p1.getX();
				float y1 = (float)p1.getY();
				p2 = da.getViewPoint(prt.getCtrl2());
				float x2 = (float)p2.getX();
				float y2 = (float)p2.getY();
				p3 = da.getViewPoint(prt.getP2());
				float x3 = (float)p3.getX();
				float y3 = (float)p3.getY();
				curve.curveTo(x1,y1,x2,y2,x3,y3);
				draw = new CubicCurveDraw(last, p1, p2, p3, (GCubicCurve)obj);
				angle = draw.getAngle();
				last = p3;
			}
		}
		g2.draw(curve);
		
		ArrowDraw ad = new ArrowDraw.Out(last, angle, 0.8);
		ad.draw(g2);
		
		Color currentColor = g2.getColor();
		if (isSelected()) {
			g2.setColor( Color.red );
			g2.draw(curve);
			ad.draw(g2);
		}
		g2.setColor(currentColor);
	}
	
//*****************************************************************************
	
	/**
	 * Returns the draw bounds in device units.
	 *
	 * @return the draw bounds
	 */
	public Rectangle getBounds() {
        
        int x1 = this.getLocation().x;
        int y1 = this.getLocation().y;
        int x2 = this.getSize().width;
        int y2 = this.getSize().height;
        
        Rectangle rect = new Rectangle(x1, y1, x2 - x1, y2 - y1);
        
        return rect;
    }
    
//*****************************************************************************
	
	public boolean contains(Point p) {
		GeneralPath curve = getCurve();
		curve.closePath();
		return curve.contains(p);
	}
	
//*****************************************************************************
	
	public GeneralPath getCurve() {
		GCurve gcv = (GCurve)getRepresents();
		GeneralPath curve = new GeneralPath();
		Point2D first = gcv.getFirstPoint();
		Point last = da.getViewPoint(first);
		Point p1, p2, p3;
		float x4 = (float)last.getX();
		float y4 = (float)last.getY();
		curve.moveTo(x4,y4);
		List primitives = gcv.getPrimitives();
		ListIterator prs = primitives.listIterator();
		while(prs.hasNext()) {
			Object obj = prs.next();
			if (obj instanceof GLine) {
				Line2D prt = (Line2D)obj;
				p1 = da.getViewPoint(prt.getP2());
				float x1 = (float)p1.getX();
				float y1 = (float)p1.getY();
				curve.lineTo(x1,y1);
				last = p1;
			}
			if (obj instanceof GQuadCurve) {
				GQuadCurve prt = (GQuadCurve)obj;
				p1 = da.getViewPoint(prt.getCtrl());
				float x1 = (float)p1.getX();
				float y1 = (float)p1.getY();
				p2 = da.getViewPoint(prt.getP2());
				float x2 = (float)p2.getX();
				float y2 = (float)p2.getY();
				curve.quadTo(x1,y1,x2,y2);
				last = p2;
			}
			if (obj instanceof GCubicCurve) {
				GCubicCurve prt = (GCubicCurve)obj;
				p1 = da.getViewPoint(prt.getCtrl1());
				float x1 = (float)p1.getX();
				float y1 = (float)p1.getY();
				p2 = da.getViewPoint(prt.getCtrl2());
				float x2 = (float)p2.getX();
				float y2 = (float)p2.getY();
				p3 = da.getViewPoint(prt.getP2());
				float x3 = (float)p3.getX();
				float y3 = (float)p3.getY();
				curve.curveTo(x1,y1,x2,y2,x3,y3);
				last = p3;
			}
		}
		return curve;
	}

//*****************************************************************************
}
